package com.hlf.modules.merchant.store.controller;


import com.alibaba.fastjson.JSON;
import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.MD5Util;
import com.hlf.common.utils.RESTfulSDLHttpUtils;
import com.hlf.modules.merchant.store.constant.XdLSpdbConstants;
import com.hlf.modules.merchant.store.entity.OnlineBusiness;
import com.hlf.modules.merchant.store.service.OnlineBusinessXDLService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/onlineBusinessXDL")
public class OnlineBusinessXDLController {

	@Autowired
	private OnlineBusinessXDLService onlineBusinessService;



	@ResponseBody
	@RequestMapping(value = "/findFirstTypeList", method = RequestMethod.GET)
	public List<OnlineBusiness> findFirstTypeList(OnlineBusiness onlineBusiness) {
		 List<OnlineBusiness> list= onlineBusinessService.findFirstTypeList(onlineBusiness);
		return list;
	}
	@ResponseBody
	@RequestMapping(value = "/findSecondTypeList", method = RequestMethod.GET)
	public List<OnlineBusiness> findSecondTypeList(OnlineBusiness onlineBusiness) {
		 List<OnlineBusiness> list= onlineBusinessService.findSecondTypeList(onlineBusiness);
		return list;
	}
	@ResponseBody
	@RequestMapping(value = "/findThirdTypeList", method = RequestMethod.GET)
	public List<OnlineBusiness> findThirdTypeList(OnlineBusiness onlineBusiness) {
		 List<OnlineBusiness> list= onlineBusinessService.findThirdTypeList(onlineBusiness);
		return list;
	}

    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.GET)
    public String insert() {
        JSONObject obj = new JSONObject(); 
        obj.put("serviceId","6060203");  // 1   String  7   交易码 6060601
        obj.put("orgNo","518");  //  //   1   String  6   机构号 合作商机构号
        String string1 = "";
        Collection<String> keyset = obj.keySet();
        List list = new ArrayList<String>(keyset);
        Collections.sort(list);
        //这种打印出的字符串顺序和微信官网提供的字典序顺序是一致的
        for (int i = 0; i < list.size(); i++) {
            if("prov_nm".equals(list.get(i)) || 
                   "city_nm".equals(list.get(i))){                    
            }else{
                string1 = string1 + obj.get(list.get(i)); 
                System.out.println(list.get(i)+"======="+obj.get(list.get(i)));
            }
        }
        String do_sign = string1 + XdLSpdbConstants.SECRET_KEY;
        System.out.println(do_sign);
        //md5加密 并转成小写
        String sign = MD5Util.MD5Encode(do_sign, "utf-8").toLowerCase(); //
        obj.put("signValue", sign);// MD5加密            
        String content = obj.toString();
        System.out.println(content);
        String returnstr = RESTfulSDLHttpUtils.sendPost(XdLSpdbConstants.REPORT_URL, content);
        System.out.println(returnstr);
        com.alibaba.fastjson.JSONObject json = JSON.parseObject(returnstr);
        String  code = json.getString("msg_cd");
        String jsonMessage = "";
           if("000000".equals(code)){
               jsonMessage = json.getString("REC");  
               JSONArray jsonArray = JSONArray.fromObject(jsonMessage);
               if(json.size()>0){
                   for(int i=0;i<jsonArray.size();i++){
                     JSONObject job = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                     OnlineBusiness OnlineBusiness = new OnlineBusiness();
                     OnlineBusiness.setBizId(Long.valueOf((String) job.get("mcc_cd")));
                     OnlineBusiness.setThirdType((String) job.get("mcc_nm"));
                     OnlineBusiness.setFirstType((String) job.get("sup_mcc_nm"));
                     OnlineBusiness.setSecondType((String) job.get("mcc_typ_nm"));
                     onlineBusinessService.insertSelective(OnlineBusiness);
                   }
                 }
           }else{
               throw new BusinessException(json.getString("msg_dat"));
           }
        return returnstr;
    }


}
